/* ======================================  表单数据行操作  ====================================== */
const _ = {
	// ======================================================================== 路由跳转
	push(_this,path,param){ _this.props.history.push(path,param) },
	replace(_this,path,param){ _this.props.history.replace(path,param) },
	back(_this){_this.props.history.goBack()},
	// 刷新路由
	refreshRouter(){ window.proxy.refresh(); },
	backRefresh(){ setTimeout(window.proxy.refresh,100) },
	// 根据根目录跳转
	pushRoot(_this,url){ _.push(_this, _.getRoot().root + url) },
	replaceRoot(_this,url){ _.replace(_this, this.getRoot().root + url) },
	// 获取检测中心路由参数
	getCenterQuery(){
		let hash = window.location.hash.split('/')
		return { result_model: hash[3], project_id: hash[4] }
	},
	pushCenter(_this, url){
		const { result_model, project_id } =  this.getCenterQuery()
		const path = window.location.hash.indexOf('/entrust-testing-center/') > -1 ? 'entrust-testing-center' : 'testing-center'
		_.pushRoot(_this,`/${path}/${result_model}/${project_id}${url}`)
	},
	// 获取当前完整路由
	getUrl(){
		let url = window.location.hash
			url = url.replace('#','')
			url = url.replace(/\?.+/g,'')
		return url + '/'
	},
	// 获取当前模块路由
	getPath(){
		let url = _.getUrl()
		return url.replace(/\/([^/]+)\//, '')
	},
	// 
	pushTo(_this, url){
		url = _.getUrl() + url
		_.push(_this, url)
	},
	// 获取 router | 获取多个参数
	getRoot(n){
		const hash = window.location.hash
		const arr = hash.split('/') 
		if (n) {
			let root = '/' 
			arr.forEach((i, index) => {
				if (index > 0 && index <= n) {
					root += i + '/'  
				}
			}) 
			return {
				root : root, 
			}
		} else { 
			return {
				root : '/' + arr[1],
				munu : arr[1]
			}
		}
	},
	// 处理路由数据
	getRouter(data){
		const { root } = this.getRoot()
		const _data = JSON.parse(JSON.stringify(data))
		const deep = data => {
			data.forEach((v,i)=>{
				const _path = v.path
				if(_path){
					v.path = root + _path
					
					if(!v.component){
						if(this.isValid(v.web_route)){
							v.component = v.web_route.replace('/','')
						}else{
							v.component = _path.replace('/','')
						}
					}
				}
				
				const children = v.children
				if(this.hasArray(children)){
					deep(children)
				}
			})
		}
		deep(_data)
		return _data
	},
	// 获取对应用菜单列表
	getMenu(name, child){
		const menu = window.$fn.local('menu')
		const { $fn } =  window
		if($fn.hasArray(menu)){
			const arr = menu.filter( v => v['menu_en'] === name )
			if($fn.hasArray( arr )){
				const data = arr[0].children || []
				// 动态添加子页面
				if($fn.hasArray( child )){	
					child.forEach( vc =>{	// 第一层
						const _arr = data.filter( m => m['menu_en'] === vc.id)
						vc.children.forEach( p => {	// 第二层
							if(!p.component){
								p.component = vc.parent.replace('/','') + '/' + p.path
							}
							p.cache = true
							if($fn.hasArray(p.children)){ // 第三层
								p.children.forEach(t => {
									t.cache = true
								})
							}
						})
						if($fn.hasArray( _arr )){
							let stack = []
							if(_arr[0].children){
								const _childArr = _arr[0].children.filter( n => n.path === vc.parent )
								if($fn.hasArray(_childArr)){
									stack =  _childArr[0]
									stack['child'] = vc.children
									stack['cache'] = true
								}
							}else{
								stack = _arr[0]
								stack['child'] = vc.children
								stack['cache'] = true
							}
						}
					})
				}
				this.setState({ data })
			}
		}
	},
	// 重新获取菜单
	resetMenu(){
		window.$http.pull(null,'menu/getallmenu',{ param:{ web_version:1 }, loading:false }).then(data=>{
			window.$fn.local('menu', data)
		})
	},
	// 重组菜单
	getLimitMenu(ids){
		const { $fn } = window
		let menu = $fn.local('menu') || []
		if($fn.hasArray(menu)){
			if(ids){
				const stack = []
				menu.forEach( v => {
					ids.forEach( m => {
						if(v['menu_en'] === m){
							stack.push(v)
						}
					})
				})
				menu = stack
			}else{
				menu = menu.filter( v => $fn.hasArray(v.children))
			}
		}
		menu.unshift({ title: '工作台',		path:'workbench/index'})
		return menu
	},
	// 页面返回刷新
	fresh: {
		// 监听/订阅刷新
		on(callback, name='onRefresh'){
			if(!window.refresh){
				window.refresh = { }
			}
			window.refresh[name] = callback
		},
		// 发布
		publish: ()=> window.refresh.onRefresh,
		// 返回
		back( name='onRefresh', param ){
			window.onpopstate = null;
			window.$fn.back(this)
			window.refresh?.[name]?.(param)
		},
		// 移除
		remove: (name='onRefresh') =>{
			if(window.refresh){
				window.refresh[name] = null
			}
		},
		backRefresh: ( name='onRefresh', param ) => {
			window.onpopstate = function(){
				window.refresh?.[name]?.(param)
			}
		}
	}
}

export default _